package com.roselism.mobilesafe.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.roselism.mobilesafe.R;
import com.roselism.mobilesafe.library.SettingLayout;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 设置界面
 */
public class SettingActivity extends AppCompatActivity {

    @Bind(R.id.settinglayout) SettingLayout settinglayout;
    private SharedPreferences mSharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);

        mSharedPreferences = getSharedPreferences("config", MODE_PRIVATE);
        boolean isAutoUpdate = mSharedPreferences.getBoolean("auto_update", true);
        settinglayout.setTitle("开启自动更新");

        if (isAutoUpdate) {
            settinglayout.setDesc("自动更新已开启");
            settinglayout.setChecked(true);
        } else {
            settinglayout.setDesc("自动更新已关闭");
            settinglayout.setChecked(false);
        }

        settinglayout.setOnClickListener(new View.OnClickListener() {// 给setting layout设置监听器事件
            @Override
            public void onClick(View v) {
                if (settinglayout.reverseChecked()) {
                    settinglayout.setDesc("自动更新已开启");
                    mSharedPreferences.edit().putBoolean("auto_update", true).commit(); // 不要忘了提交
                } else {
                    settinglayout.setDesc("自动更新已关闭");
                    mSharedPreferences.edit().putBoolean("auto_update", false).commit(); // 不要忘了提交
                }
            }
        });

    }
}